<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue.set的使用</title>
    <!--引入Vue-->
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--准备好一个容器-->
<div class="root">
    <h1>学校信息</h1>
    <h2>学校名称：{{school.name}}</h2>
    <h2>学校地址：{{school.address}}</h2>
    <h2>校 长：{{school.leader}}</h2>
    <hr/>
    <h1>学生信息</h1>
    <h2>姓名：{{student.name}}</h2>
    <h2>年龄：真实：{{student.age.realAge}}岁,对外：{{student.age.sayAge}}岁</h2>
    <h2 v-if="student.sex==0||student.sex==1">性别：{{student.sex == 0 ? '男' : '女'}}</h2>
    <h2>朋友：</h2>
    <h2 v-for="(f,index) in student.friends">姓名：{{f.name}} 年龄：{{f.age}}</h2>
    <button @click="addSex" v-if="addSexBtn">添加一个性别属性，默认为男</button>
    <button @click="delSex" v-else>删除性别属性</button>
    <button @click="changeSex" v-if="student.sex==0||student.sex==1">切换性别</button>
</div>


<script type="text/javascript">
    Vue.config.productionTip = false //阻止Vue在启动时生成提示

    new Vue({
        el: '.root',
        data: {
            school: {
                name: '尚硅谷',
                address: '北京',
                leader: '罗校长'
            },
            student: {
                name: '张三',
                age: {
                    realAge: 25,
                    sayAge: 20,
                },
                friends: [{
                    name: 'jerry',
                    age: 24
                }, {
                    name: 'tom',
                    age: 23
                }]
            },
            addSexBtn: true
        },
        methods: {
            addSex() {
                // Vue.set(this.student,'sex',0);
                this.$set(this.student, 'sex', 0);
                this.addSexBtn = false;
            },
            delSex() {
                this.$delete(this.student, 'sex');
                this.addSexBtn = true;
            },
            changeSex() {
                this.student.sex = !this.student.sex;
            }
        }


    })
</script>
</body>
</html>